package admin.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TreeMap;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import models.DealModel;
import models.ProductModel;
import admin.data.DealData;
import admin.forms.DealForm;
import data.CountData;

@SuppressWarnings("serial")
public class EditDealServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger(EditDealServlet.class
			.getName());

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		try {
			response.setCharacterEncoding("UTF-8");
			String id = request.getParameter("id");
			HttpSession session = request.getSession();
			String error = (String) session.getAttribute("error");
			if (error != null && error.length() > 0) {
				request.getRequestDispatcher("/admin/pages/form_deal.jsp")
						.forward(request, response);
			} else {
				DealModel deal = DealData.getById(id);
				session.setAttribute("action", "edit");
				if (deal != null) {
					session.setAttribute("deal", deal);
					request.getRequestDispatcher("/admin/pages/form_deal.jsp")
							.forward(request, response);

				} else {
					session.setAttribute("error",
							"Deal không tồn tại hoặc đã bị xoá !");
					response.sendRedirect("/admin/deal/list");
				}
			}
		} catch (Exception e) {
			System.out.println("Error when Dispatcher edit deal page !");
			log.warning("Error when Dispatcher edit deal page !");
			log.warning(e.toString());
			e.printStackTrace();
		}
	}

	@SuppressWarnings({ "rawtypes" })
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		response.setCharacterEncoding("UTF-8");
		String id = request.getParameter("id");
		DealModel deal = DealData.getById(id);
		// boolean changeCity = false;
		// boolean changeCate = false;
		// String oldCity = "";
		// String oldCate = "";
		if (deal != null) {
			TreeMap check = new TreeMap();
			List<ProductModel> listProduct = new ArrayList<ProductModel>();
			new DealForm().checkValid(request, check, deal, listProduct);
			HttpSession session = request.getSession();
			session.setAttribute("action", "edit");
			if (check.size() != 0) {
				session.setAttribute("check", check);
				session.setAttribute("deal", deal);
				session.setAttribute("error",
						"Không thể thêm mới Deal do các lỗi dưới đây !");
			} else {
				if (deal.getDealType() == 1) {
					DealModel dealMarketing = DealData.getDealMarketing(
							deal.getCity(), deal.getCategory());
					if (dealMarketing != null&&dealMarketing.getDealId().length()>0) {
						dealMarketing.setDealType(0);
						dealMarketing.setDateToOrder(Calendar.getInstance()
								.getTime());
						DealData.insert(dealMarketing);
					}				
					deal.setDateToOrder(Calendar.getInstance().getTime());
				}

				deal.setUpdate(Calendar.getInstance().getTime());
				DealData.insert(deal, listProduct);
				CountData countData = new CountData();
				countData.recountAll();
				session.setAttribute("success",
						"Sửa thông tin deal thành công !");
			}
			response.sendRedirect("/admin/deal/edit?id=" + deal.getDealId());
		} else {
			HttpSession session = request.getSession();
			session.setAttribute("error", "Deal không tồn tại hoặc đã bị xoá !");
			response.sendRedirect("/admin/deal/list");
		}

	}
}
